<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="com.DBCon" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.text.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询历史记录</title>
<%@ include file="../bootstrap.jsp"%>
</head>
<body>
<%
    String DepName=request.getParameter("DepName");
    String Date=request.getParameter("Date");
    String sql;
    if(DepName!=null && Date!=null){
    	sql="SELECT TeaName,DepName,DATE(teastatistics.DATE) AS DATE,IsCheck,IsUnusual FROM teastatistics WHERE"
    			+" DepName='"+DepName+"' and left(DATE,10)='"+Date+"'";
    }else{
    	sql="SELECT teacher.TeaName,teacher.DepName,DATE(teastatistics.DATE) AS DATE,IsCheck,IsUnusual FROM teacher,teastatistics WHERE teacher.TeaId=teastatistics.TeaId";
    }
    //查询打卡数据
    String sql2="Select DepName from department";
    String sql3="Select Date from teastatistics";
	try{
	ResultSet rs = new DBCon().find(sql);
	ResultSet rs2=new DBCon().find(sql2);
	ResultSet rs3=new DBCon().find(sql3);
	//获取页码
	int item = 7; //每一页显示数据项
	int pg = 1; //初始页码
	String p1 =(String)request.getAttribute("p");
	String p2 =request.getParameter("p");
	//如果接收的是页面传递的参数(Parameter 返回的一定是字符串)
	if (p1 != null && p1.length() > 0) {
		pg = Integer.parseInt(p1);
	}else if(p2 != null && p2.length() > 0){
		//如果接收的是Servlet传递的参数(Attribute 返回的是一个对象)
		pg = Integer.parseInt(p2);
	}
	//计算起始位置和结束位置
	rs.last();
	int count = rs.getRow(); //获取全部记录行数
	System.out.println(count);

	int start = (pg - 1) * item;
	int limit = pg * item < count ? pg * item : count;
	if (start > 0) {
		rs.absolute(start);
	} else {
		rs.beforeFirst();
	}
	System.out.println(start + "-" + limit);
	%>
	<h4 style="font-weight: bolder; left: 20px">历史记录列表</h4>
		<nav class="navbar navbar-light bg-light">
			<ul class="pagination">
				<!-- 显示上一页和下一页分页按钮 -->
				<%
				if (pg == 1) {
				%>
				<li class="page-item disabled "><a class="page-link">上一页</a></li>
				<%
				}
				if (pg > 1) {
				%>
				<li class="page-item "><a class="page-link"
					href="SearchRecord.jsp?p=<%=pg - 1%>">上一页</a></li>
				<%
				}
				if (pg * item < count) {
				%>
				<li class="page-item"><a class="page-link"
					href="SearchRecord.jsp?p=<%=pg + 1%>">下一页</a></li>
				<%
				} else if (pg * item >= count) {
				%>
				<li class="page-item disabled"><a class="page-link">下一页</a></li>
				<%
				}
				%>
		</ul>
    <form class="form-inline my-2 my-lg-0" action="SearchRecord.jsp">
			<select name="DepName" id="InputDepConEmail2"
				class="form-control" required>
				<%
				while(rs2.next()){
					String depname=rs2.getString("DepName");
					if(DepName!=null && rs2.getString("DepName").equals(DepName)){
						out.println("<option value='"+rs2.getString("DepName")+"' selected>"+depname+"</option>");
					}else{
					out.println("<option value='"+rs2.getString("DepName")+"'>"+depname+"</option>");
				}
				}
				%>
			</select>
			<%
			if(Date!=null){
			
			%><input name="Date" value="<%=Date%>" class="form-control mr-sm-2" type="search" placeholder="如：2022-12-19" aria-label="Search"
			   required>
			<%}else {%>
			<input name="Date"class="form-control mr-sm-2" type="search" placeholder="如：2022-12-19" aria-label="Search"
			   required>
			<%} %>
			<button class="btn btn-outline-success my-2 my-sm-0" type="submit">查询</button>
    </form>
		</nav>
	<table class="table table-striped">
		<tr align="center">
		    <th scope="col">日期</th>
			<th scope="col">学院名称</th>
			<th scope="col">教师名称</th>
			<th scope="col">是否打卡</th>
			<th scope="col">异常情况</th>
		</tr>
		<%
		while (rs.next() && rs.getRow() <= limit) {
		%>
		<tr align="center">
			<td><%=rs.getString("Date")%></td>
			<td><%=rs.getString("DepName")%></td>
			<td><%=rs.getString("TeaName")%></td>
			<td><%=rs.getString("IsCheck")%></td>
			<td><%=rs.getString("IsUnusual")%></td>
		</tr>
		<%}
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}%>
	</table>
</body>
</html>